<template>
	<view>
		<titlebar mode="我的爱车" :backToIndex="false" :back="true" :text="text" backgroundColor='#f5f5f5'>
			<view slot="title">
				<view class="showMore titleCommentTouched" @click="addCar">
					新增
				</view>
				<view class="titleText">{{text}}</view>
			</view>
		</titlebar>


		<view class="releaseState" :style="{'top':windowHeight+'px','height':'calc(100% - '+windowHeight+'px)'}">

			<scroll-view style="height: calc(100% - 16px);" scroll-y="true" class="scroll-Y" v-if="dataList.length>0"
				:refresher-enabled='false'>
				<view>


					<block v-for="(item, index) in dataList" :key="index" v-if="dataList.length>0">
						<view :data-id="item.id" :data-item="item" @click="toMyCarDetail(item)">

							<view class="list-cell-up">
								<view class="layout-up">

									<image class="car-img"
										:src="item.imageTestCover||'@appstatic/img/my/appoint_img_small.png'">
									</image>

									<view class="list-content_text">

										<view class="text-1" v-if="item.vehicleName==undefined||item.vehicleName==null||item.vehicleName == ''">{{item.modelName}}</view>
										<view class="text-1" v-else>{{item.vehicleName}}</view>


										<view class="text-2">
											车架号：{{item.vin}}</view>
										<view class="text-2" style="margin-top: 4px;">
											购买时间：{{item.produceTime}}</view>

									</view>
								</view>
							</view>
							<view class="list-cell-down">

								<view class="layout-2">
									<view class="layout-personal-info" v-if="item.isDefault==1"
										@click.stop="defaultCar(item)">
										<image class="choose-icon" src="@appstatic/car/choose_icon.png">
											<view class="text-4">设为当前用车</view>
									</view>
									<view class="layout-personal-info" v-else @click.stop="defaultCar(item)">
										<image class="choose-icon" src="@appstatic/car/unchoose_icon.png">
											<view class="text-4">设为当前用车</view>
									</view>
								</view>
							</view>

						</view>




					</block>
					<view class="noMore" v-if="pagination.loadMore == false">
						<text>没有更多数据了</text>
					</view>
				</view>

			</scroll-view>
		</view>

	</view>
</template>

<script>
	const app = getApp();
	const util = require('@utils/util.js');
	const network = require('@manager/network-manager.js');
	import titlebar from '@/components/titlebar/titlebar'
	export default {
		data() {
			return {
				windowHeight: app.globalData.statusBarHeight + 44 + 10,
				dataList: [],
				text: '我的爱车',

				title: '是否删除地址？',
				show: false,
				planDeleteId: '',
				pagination: {
					page: 1,
					totalPage: 1,
					limit: 500,
					length: 100,
					loadMore: true,
				},
				deviceId: ''
			}
		},
		components: {
			titlebar
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {

		},
		onShow: function() {
			var that = this;
			that.getMyCarList()
		},
		methods: {
			//查询我的爱车
			getMyCarList: function() {
				var that = this;
				var uri = app.globalData.config.interfaces.URL_CMOBILE_VEHICLEINFO_GETMYCARLIST;
				var params = {
					userId: app.getUserId()
				};
				console.log(params, 'params')
				network.get(uri, params, 'form').then(res => {
					if (res.code == 200) {
						that.dataList = res.data

					} else {
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none'
						})
					}
				})
			},
			//新建爱车
			addCar: function(e) {
				uni.navigateTo({
					url: "/module_car/myCarEdit/myCarEdit?from=add",
				});
			},
			//爱车详细
			toMyCarDetail: function(item) {
				uni.navigateTo({
					url: "/module_car/myCarEdit/myCarEdit?vin=" + item.vin,
				});
			},



			//默认车辆
			defaultCar: function(e) {
				var that = this
				var list = that.dataList
				for (var i = 0; i < list.length; i++) {
					if (list[i].id == e.id) {
						if (list[i].isDefault == 1) {
							list[i].isDefault = 0
						} else {
							if (list[i].deviceId == null || list[i].deviceId == '') {
								uni.showToast({
									title: "该车辆暂未绑定设备",
									duration: 3000,
									icon: 'none'
								})
							} else {
								list[i].isDefault = 1
							}
						}

					} else {
						list[i].isDefault = 0
					}
				}
				this.setDefaultCar(e)

			},
			//设为当前用车
			setDefaultCar: function(item) {
				var that = this;
				var uri = app.globalData.config.interfaces.URL_CMOBILE_VEHICLEINFO_SETDEFAULTCAR;
				var params = {
					id: item.id,
					userId: app.globalData.userInfo.id
				};
				network.get(uri, params, 'json').then(res => {
					if (res.code == 200) {
						that.deviceId = item.deviceId
						uni.setStorageSync('deviceId', that.deviceId)
					} else {
						setTimeout(() => {
							uni.showToast({
								title: res.message,
								duration: 3000,
								icon: 'none'
							})
						}, 500)
					}
				}).catch(res => {
					
					setTimeout(() => {
						uni.showToast({
							title: res.errMsg,
							duration: 3000,
							icon: 'none'
						})
					}, 500)
				});
			},







		}
	}
</script>

<style>
	@import "./myCarList.css";
</style>
